package com.xy.iot.service.impl;

import com.xy.iot.serial.port.service.RxtxClientService;
import com.xy.iot.serial.port.service.impl.RxtxClientServiceImpl;
import com.xy.iot.service.NettySerialPortRxtxClientService;
import com.xy.iot.config.properties.IotSerialPortProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: niuniu
 * @Date: 2024/6/11 20:41
 * @Description: Rxtx组件的串口服务实现类
 */
@Service
public class NettySerialPortRxtxClientServiceImpl implements NettySerialPortRxtxClientService {
    @Autowired
    private IotSerialPortProperties iotSerialPortProperties;
    private final RxtxClientService rxtxClientSerialPortService = new RxtxClientServiceImpl();
    @Override
    public void closeSerialPort() {
        rxtxClientSerialPortService.stop();
    }

    @Override
    public void serialPortSendData(byte[] bytes) {
        rxtxClientSerialPortService.writeAndFlush(bytes);
    }

    @Override
    public void init() {
        rxtxClientSerialPortService.init(iotSerialPortProperties.getThread());
        rxtxClientSerialPortService.openComPort(
                iotSerialPortProperties.getPort(),
                iotSerialPortProperties.getTimeout(),
                iotSerialPortProperties.getBaudRate(),
                iotSerialPortProperties.getDataBits(),
                iotSerialPortProperties.getStopBits(),
                iotSerialPortProperties.getParity()
        );
    }
}
